package FWK.core;

import java.awt.Cursor;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JInternalFrame;

import org.apache.log4j.Logger;

import FWK.util.ActionType;

public abstract class FwkInternalView extends JInternalFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4394621280456425282L;
	private static Logger logger = Logger.getLogger(FwkInternalView.class);

	private FwkController controller = null;
	protected Map<String, Object> request = new HashMap<String, Object>();

	public FwkInternalView() {
		this.setClosable(true);
		this.setResizable(false);
		this.setIconifiable(false);
		this.setClosable(false);
		instanciarComponentes();
		build();
		addListeners();
		pack();
		this.setVisible(true);
	}

	public void setController(FwkController controller) {
		this.controller = controller;
	}

	public FwkController getController() {
		return controller;
	}

	public void standBy() {
		this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	}

	public void activate() {
		this.setCursor(Cursor.getDefaultCursor());
	}

	protected void excecute(final FwkForm form, final ActionType action, final Map<String, Object> request) {
		controller.excecute(form, action, request, this);
	}

	protected void excecute(FwkForm form, ActionType action) {
		excecute(form, action, null);
	}

	protected void excecute(ActionType action) {
		excecute(action, null);
	}

	protected void excecute(ActionType action, Map<String, Object> request) {
		excecute(null, action, request);
	}

	protected abstract void addListeners();

	public abstract void initialize(Map<String, ? extends Object> response);

	public abstract void update(Map<String, Object> response);

	protected abstract void instanciarComponentes();

	protected abstract void build();

	protected abstract void driveException(Exception e);

	protected void volver(Map<String, Object> response) {
		excecute(ActionType.VOLVER, response);
		this.dispose();
	}

	@Override
	public void dispose() {
		controller.close();
		super.dispose();
	}
}
